package sieve

import (
	"context"
	"fmt"
	"github.com/google/wire"
	"go.manyji.com/lib/util/convert"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/schema"
	schemaSieve "sieve_admin_server/app/schema/sieve"
	"sieve_admin_server/app/server"
	serviceSieve "sieve_admin_server/app/service/sieve"
)

var _ util.API = (*TaskListAPI)(nil)

var TaskListSet = wire.NewSet(wire.Struct(new(TaskListAPI), "*"))

type TaskListAPI struct {
	util.EmptyAPI

	SieveTaskSrv *serviceSieve.SieveTaskSrv
}

func (a *TaskListAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "tasks",
			Title: "筛料任务", Perms: []*server.Perm{
				{Name: "query", Title: "查询"},
				{Name: "add", Title: "新增"},
				{Name: "download", Title: "下载"},
				{Name: "op", Title: "引擎操作"},
			},
			Order: 7,
		},
	}
}

func (a *TaskListAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("start", true),
				contextx.NewVar("end", true),
				contextx.NewVar("country"),
				contextx.NewVar("country_id", true),
				contextx.NewVar("warehouse_id", true),
				contextx.NewVar("warehouse_name"),
				contextx.NewVar("user_name"),
				contextx.NewVar("status", true),
				contextx.NewVar("task_name"),
				contextx.NewVar("warehouse_type", true),
				contextx.NewVar("business_type", true),
				contextx.NewVar("sieve_source_name"),
			},
		)
		extraArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("op"),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.SieveTaskSrv.QueryOrExport(context.Background(), ctx.Uid, reqArgs, extraArgs, p)
}

func (a *TaskListAPI) API_download_items(ctx *server.Context) interface{} {

	var (
		data = new(schemaSieve.SieveDownParams)
	)

	if ctx.Args.Has("ids") && ctx.Args.Get("ids") != "" {
		data.IDs = ctx.Args.Get("ids")
	}

	if ctx.Args.Has("file_type") && ctx.Args.Get("file_type") != "" {
		data.FileType = convert.Atoi(ctx.Args.Get("file_type"))
	}

	if ctx.Args.Has("batched") && ctx.Args.Get("batched") != "" {
		data.Batched = convert.Atoi(ctx.Args.Get("batched"))
	}

	if ctx.Args.Has("batched_file_count") && ctx.Args.Get("batched_file_count") != "" {
		data.BatchedFileCount = convert.Atoi(ctx.Args.Get("batched_file_count"))
	}

	if ctx.Args.Has("export_format") && ctx.Args.Get("export_format") != "" {
		data.ExportFormat = convert.Atoi(ctx.Args.Get("export_format"))
	}

	if ctx.Args.Has("export_type") && ctx.Args.Get("export_type") != "" {
		data.ExportType = convert.Atoi(ctx.Args.Get("export_type"))
	}

	if ctx.Args.Has("export_mode") && ctx.Args.Get("export_mode") != "" {
		data.ExportMode = convert.Atoi(ctx.Args.Get("export_mode"))
	}

	if ctx.Args.Has("number_of_single") && ctx.Args.Get("number_of_single") != "" {
		data.NumberOfSingle = convert.Atoi(ctx.Args.Get("number_of_single"))
	}

	if ctx.Args.Has("warehouse_type") && ctx.Args.Get("warehouse_type") != "" {
		data.WarehouseType = convert.Atoi(ctx.Args.Get("warehouse_type"))
	}

	if ctx.Args.Has("export_kind") && ctx.Args.Get("export_kind") != "" {
		data.ExportKind = convert.Atoi(ctx.Args.Get("export_kind"))
	}

	if ctx.Args.Has("age_range") && ctx.Args.Get("age_range") != "" {
		data.AgeRange = ctx.Args.Get("age_range")
	}

	if ctx.Args.Has("business_type") && ctx.Args.Get("business_type") != "" {
		data.BizTy = convert.Atoi(ctx.Args.Get("business_type"))
	}

	if ctx.Args.Has("platform") && ctx.Args.Get("platform") != "" {
		data.Platform = ctx.Args.Get("platform")
	} else {
		data.Platform = serviceSieve.BizTyMap[data.WarehouseType]
	}

	if ctx.Args.Has("operator") && ctx.Args.Get("operator") != "" {
		data.Operator = ctx.Args.Get("operator")
	} else {
		data.Operator = map[string]string{
			"1_3":  "gender",
			"5_2":  "linkedinGenderDown",
			"5_3":  "linkedinSeniorDown",
			"6_1":  "viberDown",
			"6_2":  "viberActiveDown",
			"7_1":  "lineDown",
			"7_3":  "lineGender",
			"8_3":  "zaloGender",
			"11_2": "tgActiveDown",
			"17_1": "operatorStandard",
			"17_2": "operatorPremium",
			"19_1": "excelCSV",
			"19_2": "excelCSV",
		}[fmt.Sprintf("%v_%v", data.WarehouseType, data.BizTy)]
	}

	if ctx.Args.Has("download_operator") && ctx.Args.Get("download_operator") != "" {
		data.DownLoadOperator = ctx.Args.Get("download_operator")
	} else {
		data.DownLoadOperator = data.Operator
	}

	return a.SieveTaskSrv.Download(context.Background(), data)
}
